<?php
namespace app\home\controller;

use \app\home\model\Category as CatModel;
use \app\home\model\Article as ArtModel;
use \app\home\model\Comment as Comment;

use \think\Validate as Validate;
use \think\Session as Session;

class Article extends Home{
    public function index(){
        return $this->fetch();
    }
    public function read(){
        $cat_id = $this->req->get('id','int');
        if(isset($cat_id)){
            $model = new CatModel;
            $cat = $model->where('cat_id',$cat_id)->find();
            $model = new ArtModel;
            $docs = $model->where('art_cat_id',$cat_id)->select();
            if(empty($cat) || empty($docs)){
                echo "<script>alert('没有文章')</script>";
                
            }else{
                $this->assign(['cat'=>$cat,'docs'=>$docs]);
                return $this->fetch();
            }
        }else{
            $this->error('致命错误');
        }
    }

    /**
     * 获取文章内容。
     */
    public function post(){
        $id = $this->req->param('id','int');
        if(isset($id)){
            $model = new ArtModel;
            $data = $model->where('art_id',$id)->find();
            $model = new Comment;
            $comments = $model->where('art_id',$id)->select();
            if(empty($article)){
                $this->assign('comments',$comments);
                $this->assign('data',$data);
                echo  $this->fetch();
            }else{
                echo "<h2>无数据</h2>";
            }
            
        }else{
            echo "<h2>数据错误</h2>";
        }
    }

    public function thinkphp(){
        
    }

    /**
     * 添加留言；
     * 
     * @return json;
     * @date Mon Jun 18 19:28:02 2018
     */
    public function addcomment(){
        if(Session::has('stu')){
            $data = $this->req->param();
            $rules = [
                'id' => 'require',
                'content' => 'require'
            ];
            $message = [
                'id.require' => 'id不能为空',
                'content.require' => '内容不能为空'
            ];

            $validate = new Validate($rules,$message);
            $result = $validate->check($data);
            if($result){
                $datas = [
                    'cmm_rid' => Session::get('stu.id'),
                    'art_id' => $data['id'],
                    'cmm_content' => $data['content'],
                    'cmm_createtime' => time()
                ];
                $model = new Comment;
                $res = $model->save($datas);
                $res = true;
                if($res){
                    echo json_encode(['state'=>1,'msg'=>'留言成功']);
                }else{
                    echo json_encode(['state'=>0,'msg'=>'留言失败']);
                }
            
            }else{
                echo json_encode(['state'=>1,'msg'=>'请查看，你是否登录！']);
            }
            
        }else{
            echo $this->alert(0,'你没有登录');
        }
               
    }
    public function getart()
    {
        // $model = new ArtModel;
        // $docs = $model->where('art_cat_id',2)->select();
        // $json = [
        $json = ['name'=>'yao','error'=>0];


        // ];
        echo $this->alert($json);
        // ($josn);
    }
    public function getaricle()
    {
        $docs = $model->where('art_cat_id',2)->select();

    }


    public function ant(){

        return json_encode(['a'=>1,'b'=>2]);
    }


}
